package com.jelly.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jelly.dto.Result;
import com.jelly.entity.WechatUser;
import com.jelly.service.WechatUserService;
import com.jelly.util.UserHolder;

@RestController
@RequestMapping("/wechat/user")
public class WechatUserController {

	@Resource
	private WechatUserService wechatUserService;

	@GetMapping("/{openid}")
	public Result<WechatUser> findByOpenid(@PathVariable("openid") String openId) {
		WechatUser user = wechatUserService.findByOpenid(openId);
		return Result.success(user);

	}

	@PostMapping
	public Result update(@RequestBody WechatUser user) {
		wechatUserService.update(user);
		return Result.success();
	}

	@GetMapping("/get")
	public Result<WechatUser> getUser(){
		Long userId = UserHolder.getHolder();
		WechatUser user = wechatUserService.findById(userId);
		return Result.success(user);
	}
}
